package LawsonTransformJava.dao;
// Oracle concrete DAO Factory implementation
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Properties;

public class DAOOracleDB extends DAODB {
	protected static Properties prop = null;
	protected static final String file = "resources/oraconnect.properties";
	protected static String base = null;
	protected static String serverName = null;
	protected static String portNumber = null;
	protected static String sid = null;
	protected static String username = null;
	protected static String password = null;
	protected static String schema = null;
	protected static final String driverName = "oracle.jdbc.driver.OracleDriver";
  
	protected String urlOra = null;
	protected Statement stmtOra = null;
	protected Connection connectionOra = null;
	protected Statement countStmtOra = null;
	
	public DAOOracleDB() {
		try {
		    Class.forName(driverName);
		
		    //Set the Oracle Properties
		    setOracleProperties();
		    
		    urlOra = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
		    connectionOra = DriverManager.getConnection(urlOra, username, password);
		    //Allow last() method call
		    stmtOra = connectionOra.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);	    
		    countStmtOra = connectionOra.createStatement();
		} catch (ClassNotFoundException e) {
		    // Could not find the database driver
			System.out.println("ClassNotFoundException: " + e);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
	}

  // method to create Oracle connections
  public Connection getDataConnection() {
    // Use DRIVER and DBURL to create a connection
    // Recommend connection pool implementation/usage
	
	try{
		if (connectionOra == null)
		    return connectionOra = DriverManager.getConnection(urlOra, username, password);
	} catch (SQLException e) {
	    // Could not connect to the database
		System.out.println("SQLException: " + e);
	}
	return connectionOra;
  }
  
  public Statement getDataStatement() {
	  try {
		  if (stmtOra == null)
			  return stmtOra = getDataConnection().createStatement();
	  } catch (SQLException e) {
		  // Could not connect to the database
		  System.out.println("SQLException: " + e);
	  }
	  return stmtOra;
  }
  
  public Statement getCountStatement() {
	  try {
		  if (countStmtOra == null)
			  return countStmtOra = getDataConnection().createStatement();
	  } catch (SQLException e) {
		  // Could not connect to the database
		  System.out.println("SQLException: " + e);
	  }
	  return countStmtOra;
  }
  
  public String getSchema() {
	  return schema;
  }

  public void setOracleProperties() throws IOException{
	    //Get the Base String for the Resource File
	    base = System.getenv("RESOURCE_BASE");
	    prop = new Properties();
	    prop.load(new FileInputStream(base + "/" + file));
	    
	    // Create a connection to the database
	    serverName = prop.getProperty("serverName");
	    portNumber = prop.getProperty("portNumber");
	    sid = prop.getProperty("sid");
	    username = prop.getProperty("username");
	    password = prop.getProperty("password");
	    schema = prop.getProperty("schema");
  }
}
